package com.example.smartagriculture.service.serviceImpl;

import com.example.smartagriculture.controller.DTO.AvgDTO;
import com.example.smartagriculture.entity.LightMonitoringMsg;
import com.example.smartagriculture.entity.MeteorologyMsg;
import com.example.smartagriculture.entity.Output;
import com.example.smartagriculture.mapper.LightMonitoringMsgMapper;
import com.example.smartagriculture.mapper.MeteorologyMsgMapper;
import com.example.smartagriculture.mapper.OutputMapper;
import com.example.smartagriculture.service.IMeteorologyMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * MeteorologyMsgServiceImpl 服务实现类
 * @author 张芸博
 * @since 2024-12-10
 */
@Service
public class MeteorologyMsgServiceImpl extends ServiceImpl<MeteorologyMsgMapper, MeteorologyMsg> implements IMeteorologyMsgService {
    @Resource
    MeteorologyMsgMapper meteorologyMsgMapper;
        @Resource
        LightMonitoringMsgMapper lightMonitoringMsgMapper;
        @Resource
    OutputMapper outputMapper;
    @Override

    public boolean jsonToObj(String json) {

        System.out.println(json);
        System.out.println("service:"+this);
        System.out.println("meteorologyMsgMapper:"+meteorologyMsgMapper);
        JSONObject jsonObject = new JSONObject(json);
        MeteorologyMsg meteorologyMsg = new MeteorologyMsg();


        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        meteorologyMsg.setTemperature(new Scanner(jsonObject.get("temperature")+"").nextInt());
        meteorologyMsg.setHumidity(new Scanner(jsonObject.get("humidity")+"").nextInt());
        meteorologyMsg.setRainfall(new Scanner(jsonObject.get("rainfall")+"").nextInt());
        meteorologyMsg.setDataDate(localDateTime);
        LightMonitoringMsg lightMonitoringMsg = new LightMonitoringMsg();
        lightMonitoringMsg.setLightIntensity( jsonObject.get("lightIntensity")+"");
        lightMonitoringMsgMapper.insert(lightMonitoringMsg);
        meteorologyMsgMapper.insert(meteorologyMsg);
        return true;
    }

    @Override
    public int getOutput() {
        List<AvgDTO> avgDTOList = new ArrayList<>();
        avgDTOList = meteorologyMsgMapper.getAVG();
        List<Output> outputList = new ArrayList<>();
        int sum = 0;
        for (AvgDTO avg : avgDTOList) {
            Output output = new Output();
            int temperature = 0, humidity = 0, rainfall = 0;
            if (15 >= avg.getTemperature()) {
                temperature = 355;
            } else if (25 > avg.getTemperature() && avg.getTemperature() > 15) {
                temperature = 650;
            } else if (avg.getTemperature() >= 25) {
                temperature = 450;
            }
            if (0.4 > avg.getHumidity()) {
                humidity = 350;
            } else if (0.7 > avg.getHumidity() && avg.getHumidity() >= 0.4) {
                humidity = 550;
            } else if (avg.getHumidity() >= 0.7) {
                humidity = 400;
            }
            if (0.2 > avg.getRainfall()) {
                rainfall = 350;
            } else if (avg.getRainfall() >= 0.2 && 0.5 > avg.getRainfall()) {
                rainfall = 500;
            } else if (avg.getRainfall() >= 0.5) {
                rainfall = 350;
            }
            sum = (temperature + humidity + rainfall) / 3;
            output.setAvgOutput(sum);
            output.setOrchardName(avg.getOrchardName());
            Date date = new Date();
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            output.setTime(localDateTime);
            outputList.add(output);
        }
        return sum;
    }

}
